import 'package:flutter/material.dart';

class EgRouterWithData extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("导航传参"),
      ),
      body: new Center(
        child: new RaisedButton(
          onPressed: () {
            final list = new List.generate(20, (i) => new Product("商品 $i", "this is prpduct $i"));
            Navigator.push(context, new MaterialPageRoute(builder: (context) => new EgRouterTestList(listProducts: list)));
          },
          child: new Text("传递数据"),
          textColor: Colors.teal,
        ),
      ),
    );
  }
}

class EgRouterTestList extends StatelessWidget {
  final List<Product> listProducts;

  EgRouterTestList({Key key, @required this.listProducts}) : super(key: key);

  _navigateToDetail(BuildContext context, Product product) async {
    final result = await Navigator.push(context, new MaterialPageRoute(builder: (context) => EgRouterDetail(product: product)));
    Scaffold.of(context).showSnackBar(new SnackBar(content: new Text(result)));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("产品列表"),
      ),
      body: new ListView.builder(
        itemBuilder: (context, index) {
          return new GestureDetector(
            onTap: () {
              _navigateToDetail(context, listProducts[index]);
            },
            child: new Container(
              padding: EdgeInsets.all(10),
              width: double.infinity,
              margin: EdgeInsets.all(3),
              child: new Text(listProducts[index].title),
            ),
          );
        },
        itemCount: listProducts.length,
      ),
    );
  }
}

class Product {
  final String title;
  final String description;

  Product(this.title, this.description);
}

class EgRouterDetail extends StatelessWidget {
  final Product product;

  EgRouterDetail({Key key, @required this.product}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("产品详情"),
      ),
      body: new Center(
        child: new RaisedButton(
          onPressed: () {
            Navigator.pop(context, "查看完毕");
          },
          child: new Text("${product.description}  查看完毕点击返回"),
        ),
      ),
    );
  }
}
